//控制层 
app.controller('companyController', function($scope, $controller, companyService) {

	$controller('baseController', {
		$scope: $scope
	}); //继承

	//读取列表数据绑定到表单中  
	$scope.findAll = function() {
		companyService.findAll().success(
			function(response) {
				$scope.list = response;
			}
		);
	}

	//分页
	$scope.findPage = function(page, rows) {
		companyService.findPage(page, rows).success(
			function(response) {
				$scope.list = response.rows;
				$scope.paginationConf.totalItems = response.total; //更新总记录数
			}
		);
	}

	//查询实体 
	$scope.findOne = function(id) {
		companyService.findOne(id).success(
			function(response) {
				$scope.entity = response;
			}
		);
	}

	//保存 
	$scope.save = function() {
		var serviceObject; //服务层对象  				
		if($scope.entity.id != null) { //如果有ID
			serviceObject = companyService.update($scope.entity); //修改  
		} else {
			serviceObject = companyService.add($scope.entity); //增加 
		}
		serviceObject.success(
			function(response) {
				if(response.success) {
					alert(response.message);
					window.location = "companyInfo-list.html";
				} else {
					alert(response.message);
				}
			}
		);
	}

	//批量删除 
	$scope.dele = function() {
		//获取选中的复选框			
		companyService.dele($scope.selectIds).success(
			function(response) {
				if(response.success) {
					$scope.reloadList(); //刷新列表
					$scope.selectIds = [];
				}
			}
		);
	}

	$scope.searchEntity = {}; //定义搜索对象 

	//搜索
	$scope.search = function(page, rows) {
		companyService.search(page, rows, $scope.searchEntity).success(
			function(response) {
				$scope.list = response.rows;
				$scope.paginationConf.totalItems = response.total; //更新总记录数
			}
		);
	}

	$scope.edit = function() {
		$scope.findOne(1);
	}

	//上传图片并保存课程
	$scope.uploadAndSave = function() {
		//单击隐藏按钮 触发 js 函数事件 将 JayUI 富文本编辑器的内容获取到 并转移到 另一个富文本编辑器中 
		//angular.element('#aCountEdit').click();
		//将转换后的文本域内容赋值给 对象属性
		//$scope.entity.slogan = document.getElementById("aCountEdit").value;
		
		//提交课程前先保存图片到数据库,并拿到相对路径
		//如果三个文件窗口都不等于null
		if( $scope.entity.image3 != null) {
			companyService.upload3().then(function(response) {
				//保存图片后进行课程信息和图片路径的保存
				$scope.entity.logo = response.data.data.src;
			

						$scope.save();
					});
				
			
		} 
	}

	$scope.addImage1 = function() {
		angular.element('#imageShow1').click();
	}

	$scope.addImage2 = function() {
		angular.element('#imageShow2').click();
	}

	$scope.addImage3 = function() {
		angular.element('#imageShow3').click();
	}
});